<?php

include_once('./config/config.php');
include_once('./config/check.php');

$id = empty($_GET['id']) ? '' : $_GET['id'];

//查询这个用户名是否存在，防止他验证后再次提交
$sql = "SELECT * FROM {$pre_}admin WHERE `id` = '$id'";
$admin = find($sql);

if (!empty($_POST)) {
  
  //密码
  $password = isset($_POST['password']) ? trim($_POST['password']) : '';

    // 更改密码判断
    if ($password) {
        $pwds = md5($password . $admin['salt']);
        $pwd = $pwds;
    } else{
        $pwd = $admin['password'];
    }
    

    //组装数据
    $data = [
        'username' => $admin['username'],
        'salt' => $admin['salt'],
        'password' => $pwd, //让密码和密码盐加密
    ];
    

  //先判断是否有文件上传 (错误=0=没错误  大小>0)
  if ($_FILES['avatar']['error'] == 0 && $_FILES['avatar']['size'] > 0) {

    $result = upload('avatar', 'assets/uploads/');

    if ($result['success']) {

     //删除原先的头像
      @is_file($admin['avatar']) && @unlink($admin['avatar']);
      $data['avatar'] = $result['msg'];
      

    } else {

      ShowMsg($data['msg']);
      exit;
    }
  }


  //插入到数据中
  $insert = update('admin', $data, 'id='.$id);
  
  if($insert) {
    ShowMsg('修改管理员成功','AdminList.php');
    exit;
  } else{
    ShowMsg('修改管理员失败');
    exit;
  }
}
?>



<!DOCTYPE html>
<html lang="en">

<head>
  <?php include_once('common/meta.php'); ?>
</head>

<body>


  <?php include_once('common/header.php'); ?>

  <?php include_once('common/menu.php'); ?>

  <div class="content">
    <div class="header">
      <h1 class="page-title">发布文章</h1>
    </div>
    <ul class="breadcrumb">
      <li><a href="index.html">Home</a> <span class="divider">/</span></li>
      <li class="active">Index</li>
    </ul>

    <div class="container-fluid">
      <div class="row-fluid">

        <div class="btn-toolbar">
          <button class="btn btn-primary" onClick="location='adminList.php'"><i class="icon-list"></i> 管理员列表</button>
          <div class="btn-group">
          </div>
        </div>

        <div class="well">
          <div id="myTabContent" class="tab-content">
            <div class="tab-pane active in" id="home">
              <form method="post" enctype="multipart/form-data">
                <label>用户名</label>
                <input type="text" name="username" class="input-xxlarge" value="<?php echo $admin['username'];?>"  disabled>
                <span id="notice" style="color:red;display:none;">管理员已注册</span>
                <label>密码</label>
                <input type="password" name="password" value="" class="input-xxlarge">
                <label>头像</label>
                <input type="file" name="avatar" class="input-xxlarge" />
                <label></label>
                <input id="add" class="btn btn-primary" type="submit" value="提交" />
              </form>
            </div>
          </div>
        </div>

        <div class="modal small hide fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
            <h3 id="myModalLabel">Delete Confirmation</h3>
          </div>
          <div class="modal-body">
            <p class="error-text"><i class="icon-warning-sign modal-icon"></i>Are you sure you want to delete the user?</p>
          </div>
          <div class="modal-footer">
            <button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
            <button class="btn btn-danger" data-dismiss="modal">Delete</button>
          </div>
        </div>

        <?php include_once('common/footer.php'); ?>

      </div>
    </div>
  </div>
</body>

</html>
<?php include_once('common/script.php'); ?>
<script>
  $("input[name='username']").change(function() {

    //用户名
    var username = $(this).val()


    //JS版本

    //创建异步对象
    var ajax = new XMLHttpRequest();


    //绑定请求状态  当请求被发送到服务器时，我们需要执行一些基于响应的任务
    ajax.onreadystatechange = function() {
      // console.log(ajax.readyState)
      //http的状态码
      // console.log(ajax.status)
      // if(ajax.readyState == 4)
      if (ajax.status == 200) {
        //获取到的是一个 json的字符串
        // console.log(ajax.responseText)

        //将获取到的json 变成js的数据类型
        var result = JSON.parse(ajax.responseText)

        //根据结果来判断
        if (result.success) {
          //可以注册
          $("#notice").css({
            'display': 'inline-block',
            'color': 'green'
          })
          $("#notice").html(result.msg)
          $("#add").prop('disabled', false)
        } else {
          //不可以注册
          $("#notice").css({
            'display': 'inline-block',
            'color': 'red'
          })
          $("#notice").html(result.msg)
          $("#add").prop('disabled', true)
        }
      }
    }


    //发起请求  false 同步  true 异步
    ajax.open('post', 'AdminAdd.php?action=check', true)

    //post的请求一定要设置 http头信息
    ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')

    //发送请求，并传递数据
    ajax.send(`username=${username}`)
  })
</script>